<?php
class Model_Email extends Zend_Db_Table {
	
	/**
	 * name of the table
	 *
	 * @var string
	 */
		
	protected $_name = 'hl_contact_emails';
	
	public function init() {}
	
	public function get($filters, $order = null) {
		$select = $this->select()
    					->setIntegrityCheck(false)
    					->from(array('ce' => $this->_name));
		
    	foreach($filters AS $key => $value) {
    		
    		/** TODO
    			If "extra" field
    		*/
    		
    		// If is array, IN()
    		if(is_array($value)) {
    			$temp = '';
    			foreach($value AS $val) {
    				$temp .= '"' . $val . '",';
    			}
    			$temp = trim($temp, ',');
    			
    			$select->where('LOWER(ce.' . $key . ') IN ('.$temp.')');
    		
    		// Else	
    		} else {
				$select->where('LOWER(ce.' . $key . ') = ?', $value);
			}
    	}
			
		if(!is_null($order)) {
			$select->order($order);
		}
		$result_temp = $this->fetchAll($select);
		
		$result = Model_Tools::stripslashes($result_temp);
		
    	return $result;
	}
	
	public function add($datas) {
		
		foreach($user as &$value) {
			$value = addslashes($value);
		}
		$id = $this->insert($user_tab);
		return $id;
	}
	
	public function set($id, $datas) {
		
		// addslashes
		foreach($datas as &$value) {
			$value = addslashes($value);
		}
		
    	$result = $this->update($datas, array('id = ?' => $id));
    	return $result;
		
	}
	
	public function send($datas, $to = null, $from = null) {
		
		$from = 'contact@hybridlegacy.fr';
		$to = 'contact@hybridlegacy.fr';
		
		// get subject
		$sM = new Model_Subject();
		$filter = array('id' => $datas['subject']);
		$subject = $sM->get($filter);
		
		$object = '[HYBRID LEGACY] '.$subject[0]['object'].' '.$datas['object'];
		
		$message_infos = '<h1>Informations sur le contact</h1>';
		$message_infos = '<p>';
		$message_infos = '<span style="font-weight: bold;">Prénom : </span>' . $datas['firstname'] . '</span><br />';
		$message_infos = '<span style="font-weight: bold;">Nom : </span>' . $datas['lastname'] . '</span><br />';
		$message_infos = '<span style="font-weight: bold;">Email : </span>' . $datas['email'] . '</span><br />';
		$message_infos = '</p>';
		
		// Message
		$message = '<html><head><title>'.$object.'</title></head><body>';
		$message .= $datas['message'];
		$message .= '</body></html>';
		$headers = 'From: Hybrid Legacy <'.$from.'>'."\n";
		$headers .= 'Bcc:publicite@micster.fr'."\n";
		$headers .= 'Reply-To: '.$from."\n"; 
		$headers .= 'Content-Type: text/html; charset="utf-8"'."\n";
		$headers .= 'Content-Transfer-Encoding: 8bit'."\n\n";
		
		// Envoi du mail
		$result = mail($to, $object, $message, $headers);
		
		// save in database
		$datas['is_send'] = $result;
		$id = $this->insert($datas);
		
		return $result;
		
	}
	
}
?>
